using System;
using System.Collections.Generic;
using abilities.targets;
using abilities.templates;
using UnityEngine;

namespace abilities.effects
{
    public class EffectHitData : Effect<EffectHitDataTemplate>
    {
        int damage;
        List<TargetObject> storedTargets;
        
        public EffectHitData(EffectHitDataTemplate t, int lv) : base(t, lv)
        {
        }
        ~EffectHitData()
        {
            ClearTargets();
        }

        protected override void OnInit(EffectHitDataTemplate pTemplate,int lv)
        {
            damage = template.getDamage(lv);
        }

        protected override void Apply(UnitObject objectView, bool replay)
        {
            if (replay)
            {
                return;
            }

            UnitObject owner = (UnitObject) (objectView);
            if (!owner)
                return;

            if (template.resetTargets)
            {
                ClearTargets();
                AcquireTargets(owner);
            }

            foreach (var it in storedTargets)
            {
                if (it.unit.isDead())
                {
                    continue;
                }

                TargetUnitSide side = owner.unitSide != it.unit.unitSide ? TargetUnitSide.ENEMY : TargetUnitSide.ALLY;
                TargetUnitType targetUnitType = owner.name.StartsWith("Hero") ? TargetUnitType.Hero : TargetUnitType.Pet;
                DamageType damageType = template.area > 0 ? DamageType.AREA : DamageType.SINGLE;
                HitData hitData = HitData.Create(it, damage, (float) template.area / 100.0f,damageType,side,targetUnitType, Vector2.negativeInfinity, 0);
                hitData.Source = new TargetObject(owner);

                if (template.projectile != 0)
                {
                    // AbstractProjectile projectile = ProjectilesFactory.create(owner, hitData, projectileId);
                }
                else
                {
                    //GameLogic.Instance.applyAttackHit(hitData);
                }
            }

            ClearTargets();
        }


        protected override void Cancel(UnitObject objectView, bool replay)
        {
        }

        void prepareCast(UnitObject objectView, bool replay)
        {
            if (replay)
            {
                return;
            }

            UnitObject owner = objectView as UnitObject;
            if (!owner)
            {
                return;
            }

            AcquireTargets(owner);

            if (IsTimed)
            {
                //如果时间效应的，把单位转到我们能找到的第一个目标。
                foreach (var it in storedTargets)
                {
                    if (it.unit.isDead())
                    {
                        continue;
                    }
                    owner.transform.LookAt(it.unit.transform.position);
                    break;
                }
            }
        }

        void AcquireTargets(UnitObject owner)
        {
            storedTargets = prototype.target.GetTargets(new AbilityContext(this, owner.abilityManager));
            foreach (var it in storedTargets)
            {
                // it.retain();
            }

            throw new NotImplementedException();
        }

        void ClearTargets()
        {
            foreach (var it in storedTargets)
            {
                // it.retain();
            }

            throw new NotImplementedException();
            storedTargets.Clear();
        }

    }
}